package com.hjn.json;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import org.apache.commons.lang.StringUtils;

import java.io.*;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;

public class AnalyiseJson {
    public static void main(String[] args) throws IOException {
        String path = "E:\\study\\jcode\\son\\src\\main\\doc\\json.txt";
        File jsonFile = new File(path);
        StringBuffer jsonStr = new StringBuffer("");
        InputStream inputStream = new FileInputStream(jsonFile);
        int len = 0 ;
        byte array[] = new byte[1024];
        while ((len = inputStream.read(array))!=-1) {
            String s = new String(array, 0, len,"gbk");
            // System.out.println("s = " + s);/**/
            jsonStr.append(s);
        }
        inputStream.close();
        System.out.println("jsonStr.toString() = " + jsonStr.toString());
        String s = null;
        System.out.println(s);
        JSONArray jsonArray = JSONArray.parseArray(jsonStr.toString());
        List<JSONObject> step1 = new ArrayList();
        List<JSONObject> step2 = new ArrayList();
        List<JSONObject> step3 = new ArrayList();
        List<JSONObject> step4 = new ArrayList();

       jsonArray.forEach(object ->{
           JSONObject json = (JSONObject) object;
           Set<String> strings = json.keySet();
           strings.forEach(key ->{
               String value = (String)json.get(key);
               System.out.println(key +"------" + value);
               JSONObject obj = new JSONObject();
               obj.put(key, value);
               if(key.contains("step1")){
                   step1.add(obj);
               }else if(key.contains("step2")){
                   step2.add(obj);
               }else if(key.contains("step3")){
                   step3.add(obj);
               }else if(key.contains("step4")){
                   step4.add(obj);
               }
           });
       });
        System.out.println(step1.size());
        System.out.println(step2.size());
        System.out.println(step3.size());
        System.out.println(step4.size());

        step1.forEach(jsonObject ->{
            Set<String> strings = jsonObject.keySet();
            
        });
    }
}
